home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / SORTER.C < prev    next >
C/C++ Source or Header  |  1992-03-12  |  6KB  |  198 lines

  1. /***************************************************************************
  2.  *
  3.  *    Program Name : SORTER.C
  4.  *
  5.  *    Written By : Eng-Huat Ong and Kian-Mong Low.
  6.  *
  7.  *    This program sorts cubes in the input file before minimization in
  8.  *    the order of 01X. ie. ascending order.
  9.  *
  10.  * --------------------------------------------------------------------------
  11.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  12.  *    University.
  13.  *
  14.  *    You are free to use, copy and distribute this software and its
  15.  *    documentation providing that:
  16.  *
  17.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  18.  *
  19.  *        IT IS NOT MODIFIED IN ANY WAY.
  20.  *
  21.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  22.  *
  23.  *    This program is provided "AS IS" without any warranty, expressed or
  24.  *    implied, including but not limited to fitness for any particular
  25.  *    purpose.
  26.  *
  27.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  28.  *    appreciated. Please send to:
  29.  *
  30.  *        Boon-Tiong Tan or Othman Bin Ahmad
  31.  *        School of EEE
  32.  *        Nanyang Technological University
  33.  *        Nanyang Avenue
  34.  *        Singapore 2263
  35.  *        Republic of Singapore
  36.  *
  37.  ***************************************************************************
  38.  *
  39.  *    n    == no. of variables
  40.  *    cu, cu1 == no. of cubes
  41.  *    i, k    == counters
  42.  *
  43.  ***************************************************************************/
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48.  
  49.  
  50. main()
  51.  
  52. {
  53.    unsigned char     *infile, *outfile, *np, *cp, *cube, *temp;
  54.    unsigned char     n, k, c;
  55.          int     test;
  56.    unsigned long     cu, cu1, i;
  57.    FILE              *in, *out;             /* file pointer */
  58.  
  59.    printf("This program sorts cubes in order of '01X' (ie. ascending order) before\n");
  60.    printf("subjecting them to minimization. \n\n");
  61.    printf("Press any key to Continue or <ESC> to QUIT\n\n");
  62.    c = getch();
  63.    if (c==27)
  64.       exit(0);
  65.  
  66.    infile = (unsigned char *)malloc (21);      /* space for input filename */
  67.    if (infile==0)
  68.       {
  69.      printf("Out of memory -- SORTER, *infile\n");
  70.      printf("Program Terminated - 1\n");
  71.      exit(0);
  72.       }
  73.  
  74.    outfile = (unsigned char *)malloc (21);    /* space for output filename */
  75.    if (outfile==0)
  76.       {
  77.      printf("Out of memory -- SORTER, *outfile\n");
  78.      printf("Program Terminated - 2\n");
  79.      exit(0);
  80.       }
  81.  
  82.    printf("Enter input filename -> ");
  83.    gets(infile);                                 /* get input filename */
  84.  
  85.    printf("Enter output filename -> ");
  86.    gets(outfile);                               /* get output filename */
  87.  
  88.    printf("Please hang on, sorting in process ... \n\n");
  89.  
  90.    if ((in = fopen(infile, "r+")) == NULL)          /* open input file */
  91.       {
  92.      printf("Error opening file, %s\n", infile);
  93.      printf("Program terminated.\n");
  94.      exit(0);
  95.       }
  96.  
  97.    if ((out = fopen(outfile, "w")) == NULL)        /* open output file */
  98.       {
  99.      printf("Error opening file, %s\n", outfile);
  100.      printf("Program terminated.\n");
  101.      exit(0);
  102.       }
  103.  
  104.    np = (unsigned char *)malloc(4);            /* space for no. of var */
  105.    if (np==0)
  106.       {
  107.      printf("Out of memory -- SORTER, *np\n");
  108.      printf("Program Terminated - 3\n");
  109.      exit(0);
  110.       }
  111.  
  112.    fgets(np, 4, in);                           /* get no. of variables */
  113.    n = atoi(np);                                   /* no. of variables */
  114.    free(np);                                           /* free pointer */
  115.    fprintf(out, "%d\n", n);                        /* print to outfile */
  116.  
  117.    cp = (unsigned char *)malloc(5);          /* space for no. of cubes */
  118.    if (cp==0)
  119.       {
  120.      printf("Out of memory -- SORTER, *cp\n");
  121.      printf("Program Terminated - 4\n");
  122.      exit(0);
  123.       }
  124.  
  125.    temp = (unsigned char *)malloc(n+2);           /* temporary storage */
  126.    if (temp==0)
  127.       {
  128.      printf("Out of memory -- SORTER, *temp\n");
  129.      printf("Program Terminated - 5\n");
  130.      exit(0);
  131.       }
  132.  
  133.    for (k=0; k<2; k++)
  134.       {
  135.      fgets(cp, 5, in);                         /* get no. of cubes */
  136.      cu = atoi(cp);                            /* ascii to integer */
  137.      fprintf(out, "%d\n", cu);                 /* print to outfile */
  138.  
  139.      if (cu==0)                                /* zero cube */
  140.         break;
  141.  
  142.      cube = (unsigned char *)malloc(n*cu);     /* space for all cubes */
  143.      if (cube==0)
  144.         {
  145.            printf("Out of memory -- SORTER, *cube\n");
  146.            printf("Program Terminated - 6\n");
  147.            exit(0);
  148.         }
  149.  
  150.      for (i=0; i<cu; i++)                /* read and store all cubes */
  151.         {
  152.            fgets(temp, n+2, in);
  153.            memcpy((cube+n*i), temp, n);
  154.         }
  155.  
  156.      cu1 = cu;
  157.  
  158.      while (cu-- > 1)                    /* bubble sort algorithm */
  159.         {
  160.            for (i=0; i<cu; i++)
  161.           {
  162.              test = memcmp((cube+n*i), (cube+n*(i+1)), n); /* compare adjacent minterms */
  163.  
  164.              if (test > 0)            /* 1st term > 2nd term */
  165.             {
  166.                memcpy(temp, (cube+n*i), n);          /* swap */
  167.                memcpy((cube+n*i), (cube+n*(i+1)), n);
  168.                memcpy((cube+n*(i+1)), temp, n);
  169.             }
  170.  
  171.              else if (test==0)          /* duplicate cubes */
  172.             {
  173.                memcpy((cube+n*i), (cube+n*(i+1)), n*(cu1-i)); /* remove duplicate */
  174.                i--;
  175.             }
  176.           }
  177.         }
  178.  
  179.      for (i=0; i<cu1; i++)        /* print sorted cubes to outfile */
  180.         {
  181.            memcpy(temp, (cube+n*i),  n);
  182.            *(temp+n) = '\0';
  183.            fprintf(out, "%s\n", temp);
  184.         }
  185.      free(cube);                  /* free pointer */
  186.       }
  187.  
  188.    fclose(in);                        /* close files */
  189.    fclose(out);
  190.    free(cp);                          /* free pointers */
  191.    free(temp);
  192.    free(in);
  193.    free(out);
  194.  
  195.    printf("Sorting completed.\n");
  196. }
  197.  
  198.